<script setup lang="ts">
import ArticleItem from './ArticleItem.vue'
import { getKnowledgeList } from '@/services/consult'
import type { KnowledgeList, KnowledgeParams, KnowledgeType } from '@/types/consult'
import { ref } from 'vue'

const props = defineProps<{
  type: KnowledgeType //参数类型
}>()

// 加载数据
const loading = ref(false)
const finished = ref(false)
const list = ref<KnowledgeList>([])
const requestParams: KnowledgeParams = {
  type: props.type,
  current: 1,
  pageSize: 5,
}

const onLoad = async () => {
  const res = await getKnowledgeList(requestParams)
  list.value.push(...res.data.rows)

  if (requestParams.current >= res.data.pageTotal) {
    // 停止加载
    finished.value = true
  } else {
    // 加载下一页
    requestParams.current++
  }
  loading.value = false
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      @load="onLoad"
      v-model:loading="loading"
      finished-text="我是有底线的~"
      :finished="finished"
    >
      <!--
          知识点【vant van-list 经验】：van-list的加载不仅受到loading属性的控制，也会受到容器高度的影响，如果容器高度固定，会被看做一直处于触底状态，也就是一直触发load事件
          所以，写demo的时候，避免使用下面  v-for="i in 5" 的方式，因为5是固定的，导致容器高度固定，一直处于触底状态
      -->
      <!-- <article-item v-for="i in 5" :key="i"></article-item> -->
      <article-item v-for="item in list" :key="item.id" :item="item"></article-item>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
